package com.yunfei.procedure;

import com.yunfei.api.financialCalculator.param.YangLaoCalParam;
import com.yunfei.api.financialCalculator.param.ZiNvJiaoYuCalParam;
import com.yunfei.api.financialCalculator.result.YangLaoCalResult;
import com.yunfei.api.financialCalculator.result.ZiNvJiaoYuCalResult;
import com.yunfei.utils.jdbc.AiiidbUtil;
import oracle.jdbc.OracleTypes;

import java.sql.*;

/**
 * 调用接口：
 *  jcpt_func_pkg.sp_znjsq
 * (CHLID_AGE       INTEGER, --传入子女年龄
 *  WANT_AMT        VARCHAR2, --传入需要的金额 例如： 10000,8000,13500
 * PLAN_GET_AMT OUT VARCHAR2, --输出样式：(哪一年需要攒多少钱)2019,2020,2021:20000;2022,2023,2024:30000;
 * PO_SP_RSLT_FG   OUT INTEGER,--输出参数：当输出0 时，代表计算出错，按PO_SP_RSLT_INFO输出内容，输出1时，代表正确，前台处理PLAN_GET_AMT字段，展示数据
 *  PO_SP_RSLT_INFO OUT VARCHAR2)--输出参数：当PO_SP_RSLT_FG输出0时，这个字段输出错误描述
* @Description: 子女计算器
* @Author: zhengyunfei
* @Date: 2019/2/15
* @time:16:44
*/
public class ZiNvJiaoYuCalProcedure {
    private Connection conn;
    private CallableStatement stat;
    private ResultSet rs;
    String calSql = "call jcpt_func_pkg.sp_znjsq(?,?,?,?,?)";
    /**
    * @Description: 统计计算器存储过程
    * @Author: zhengyunfei
    * @Date: 2018/4/24
    * @time:13:49
    */
    public ZiNvJiaoYuCalResult callProcedure(AiiidbUtil aiiidbUtil, ZiNvJiaoYuCalParam param) {
        ZiNvJiaoYuCalResult result=new ZiNvJiaoYuCalResult();
        try {
            String url=aiiidbUtil.getJdbcUrl();
            String driverName=aiiidbUtil.getDriveClassName();
            String username=aiiidbUtil.getJdbcUserName();
            String password=aiiidbUtil.getJdbcPassword();
            Class.forName(driverName);
            System.out.println("url======"+url);
            System.out.println("driverName======"+driverName);
            System.out.println("username======"+username);
            System.out.println("password======"+password);
            conn = DriverManager.getConnection(url, username, password);
            stat = conn.prepareCall(calSql);
            // 2个输入参数和2个输出参数
            stat.setFloat(1, Float.parseFloat(param.getChildAge()));
            stat.setString(2, param.getWantAmt());
            stat.registerOutParameter(3, Types.VARCHAR);
            stat.registerOutParameter(4, Types.VARCHAR);
            stat.registerOutParameter(5, Types.VARCHAR);
            boolean isSuccess=stat.execute();
            conn.commit();
            //返回结果
            String planGetAmt= stat.getString(3);
            String poSpRsltFg= stat.getString(4);
            String poSpRsliInfo= stat.getString(5);
            result.setPlanGetAmt(planGetAmt);
            result.setPoSpRsltFg(poSpRsltFg);
            result.setPoSpRsltInfo(poSpRsliInfo);
            System.out.println("子女教育计算器返回结果: " + result.getPlanGetAmt()+","+","+result.getPoSpRsltInfo());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn, stat, rs);
        }
        return result;
    }

    // 关闭连接
    public void close(Connection conn, CallableStatement stat, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                rs = null;
            }
        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                stat = null;
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }
    }

    public static void main(String[] args) {
      //  new ZiNvJiaoYuCalProcedure().callProcedure("20180417132146779447466");
    }
}